package com.chencs.common.menu.service;

import com.chencs.common.menu.mapper.MenuMapper;
import com.chencs.vo.common.menu.MenuModel;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fangzehua on 2017/6/14.
 */
@Service
@Transactional
public class MenuService {

    @Autowired
    private MenuMapper menuMapper;

    public List<MenuModel> getList(){
        List<MenuModel> newMenuList = new ArrayList<>();
        List<MenuModel> parentModeList = menuMapper.getParentMenu();
        for(MenuModel m: parentModeList){
            List<MenuModel> models = menuMapper.findMenuByParentCode(m.getMenuCode());
            for(int i =0;i<models.size();i++){
                models.get(i).setMenuName("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+models.get(i).getMenuName());
            }
            newMenuList.add(m);
            newMenuList.addAll(models);
        }
        return newMenuList;
    };


    public MenuModel findMenuByCode(String menu_code){
        return  menuMapper.findMenuByID(menu_code);
    };


    public boolean delete(String menu_code){
        List<MenuModel> models = menuMapper.findMenuByParentCode(menu_code);
        if(models.size()<1){
            menuMapper.delete(menu_code);
            return true;
        }else{
            return false;
        }


    }


    public boolean add(MenuModel menuModel){
        List<MenuModel> models = menuMapper.findMenuByMenuCode(menuModel.getMenuCode());
        if(models.size()==0){
            menuMapper.add(menuModel);
            return true;
        }else{
            return false;
        }
    }

    public void update(MenuModel menuModel){
        menuMapper.update(menuModel);
    }
}
